package me.wxk.interview.activemq.service;

import me.wxk.interview.activemq.constant.QueueName;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.Serializable;
import java.util.HashMap;

/**
 * @author wangxuekai
 * @version 2.3
 * @Date 2017/5/11.
 * @since 2.3
 */
public class RequestSubmit {
    //消息发送者
    private MessageProducer producer;
    private Session session;

    public void init() throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://192.168.43.129:61616");
        Connection connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(QueueName.TEST);
        // 得到消息发送者
        producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    }

    public void submit(HashMap<Serializable, Serializable> requestParams) throws JMSException {
        ObjectMessage message = session.createObjectMessage(requestParams);
        producer.send(message);
        session.commit();
    }

    public static void main(String[] args) throws Exception {
        RequestSubmit requestSubmit = new RequestSubmit();
        requestSubmit.init();

        for (int i = 0; i < 10; i++) {
            HashMap<Serializable, Serializable> requestParams = new HashMap<Serializable, Serializable>();
            requestParams.put("王学凯"+i, "wxk"+i);
            requestSubmit.submit(requestParams);
        }

    }
}
